<?php
/**
 *
 *
 *
 */
class Yz_Form_Element_ForeignKey
    extends Yz_Form_Element_Abstract
{

    protected $element;
    protected $property;
    protected $name;
    protected $options;
    protected $value;


    /**
     *  Setter: $element->value = x
     *
    public function setValue($value)
    {
        if (null !== $value and
                ! ($value instanceof Yz_Item_Interface and $value->isItemOf($this->property->getModel()))
            )
            throw Yz_Form_Element_Exception::_new('Bad value for element');
        $this->value = $value;
        return $this;
    }
     */

    public function render(Yz_Template_Interface $template)
    {
        $class = $this->getCSSClass();
        $options = $this->renderOptions($template);
        $o = <<<html
            <select name="{$this->name}" class="{$class}">
{$options}
            </select>\n
html;
        return $o;
    }


    /**
     *  @return this
     *  @throw Yz_Form_Element_Exception
     */
    public function parse($value)
    {
        $this->value = null;
        $elName = $this->name;
        debug("Element '$elName'");
        $value = Yz_Int::valueOf($value);
        if (null === $value)
            throw new Yz_Form_Element_Exception("Invalid value for element {$elName} (must be an integer number)");

        if (0 === $value) {
            if ( ! $this->allowEmpty() )
                throw new Yz_Form_Element_Exception("Empty value for element {$elName}");
            $this->value = null;
        } else {
            try {
                $this->value = $this->element->getModel()->fetchItemById($value);
            } catch (Yz_Model_Exception $ex) {
                throw new Yz_Form_Element_Exception("Value not found for element {$elName}");
            }
            #if (! $this->options->offsetExists($value))
            #    throw new Yz_Form_Element_Exception("Unrecognized value for element {$elName}");
            #$this->value = $this->options[$value];
        }
        return $this;
    }

    /**
     *
     *
     */
    protected function loadOptions()
    {
        debug();
        $element = $this->element;
        $model = $element->getModel();
        try {
            $options = $model->fetchList();
        } catch (Yz_Exception $ex) {
            $options = array();
        }
        $this->options = $options;
        return $options;
    }

    /**
     *
     *
     */
    protected function renderOptions(Yz_Template_Interface $template)
    {
        debug();
        $o = "";
        $currentValue = $this->value ? $this->value->id : 0;
        if ($this->allowEmpty()) {
            $selected = $currentValue === 0 ? ' selected="selected"' : '';
            $text = $template->translate("-- Not selected --");
            $o .= <<<html
                    <option{$selected} value="0">{$text}</option>\n
html;
        }
        $options = $this->loadOptions();
        foreach ($options as $option) {
            $text = htmlspecialchars($option->title);
            $selected = $currentValue === $option->id ? ' selected="selected"' : '';
            $o .= <<<html
                    <option{$selected} value="{$option->id}">{$text}</option>\n
html;
        }
        return $o;
    }

}
